//
//  UUDriverManager.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 23/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUDriverManager.h"
#import "UUDriverUtility.h"
#import <UUFoundation/UUIncrementDataManager.h>
#import <UUFoundation/DKDataManager.h>
#import "UUDriverRequestModel.h"
#import "UUDriverAddRequestModel.h"
#import "UUDriverEditRequestModel.h"
#import "UUDriverDeleteRequestModel.h"

@interface UUDriverManager ()

@property (nonatomic, strong) UUIncrementDataManager *driverDataManager;

@end

@implementation UUDriverManager

- (instancetype)init {
    self = [super init];
    if (self) {
        self.driverDataManager = [UUIncrementDataManager managerWithConfiguration:
                                  [UUIncrementDataManagerConfiguration configurationWithRequestModelClass:[UUDriverRequestModel class]
                                                                                           storeContainer:[UUDriverUtility driverDefaults]]];
    }
    return self;
}

- (void)asyncUpdateDrivers {
    WEAK_SELF();
    [self.driverDataManager asyncIncrementUpdateWithCompleteBlock:^(BOOL hadUpdate, NSArray<id> *insert, NSArray<id> *delete, NSError *error) {
        if (hadUpdate) {
            if (insert.count > 0) {
                [weakSelf notifyObserversWithSelector:@selector(driverManagerDidUpdateDrivers) withObject:nil];
            }
            
            if (delete.count > 0) {
                [weakSelf notifyObserversWithSelector:@selector(driverManagerDidDeleteDrivers:) withObject:delete];
            }
        }
    }];
}

- (NSArray<NSNumber *> *)fetchDrivers {
    return [self fetchDriversForFleetId:nil];
}

- (NSArray<NSNumber *> *)fetchDriversForFleetId:(NSNumber *)fleetId {
    NSDictionary *where = nil;
    if (fleetId != nil) {
        where = @{
                  @"fleetId" : fleetId
                  };
    }
    NSArray<NSNumber *> *drivers = [UUDriverModel searchColumn:[UUDriverModel getPrimaryKey]
                                                         where:where
                                                       orderBy:@"updateTime desc"
                                                        offset:0
                                                         count:0];
    return drivers;
}

- (UUDriverModel *)fetchDriverWithDriverId:(id)driverId {
    return (UUDriverModel *)[self.driverDataManager fetchModelWithPrimaryKeyValue:driverId];
}

- (void)asyncDeleteDriverWithDriverId:(NSNumber *)driverId completeBlock:(void (^)(NSError *error))completeBlock {
    UUDriverDeleteRequestModel *requestModel = [UUDriverDeleteRequestModel new];
    requestModel.driverId = driverId;
    
    WEAK_SELF();
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            [weakSelf asyncUpdateDrivers];
        }
        
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

- (void)addDriverWithFleetId:(NSNumber *)fleetId
                        name:(NSString *)name
                      mobile:(NSString *)mobile
               completeBlock:(void (^)(NSError *error))completeBlock {
    UUDriverAddRequestModel *requestModel = [UUDriverAddRequestModel new];
    requestModel.motorcadeId = fleetId;
    requestModel.name = name;
    requestModel.mobile = mobile;
    
    WEAK_SELF();
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            [weakSelf asyncUpdateDrivers];
        }
        
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

- (void)updateDriverWithDriverId:(id)driverId
                         fleetId:(NSNumber *)fleetId
                            name:(NSString *)name
                          mobile:(NSString *)mobile
                   completeBlock:(void (^)(NSError *error))completeBlock {
    UUDriverEditRequestModel *requestModel = [UUDriverEditRequestModel new];
    requestModel.driverId = driverId;
    requestModel.motorcadeId = fleetId;
    requestModel.name = name;
    requestModel.mobile = mobile;
    
    WEAK_SELF();
    [[DKDataManager new] requestWithRequestModel:requestModel completedBlock:^(DKResponseModel *responseModel, NSError *error) {
        if (error == nil) {
            [weakSelf asyncUpdateDrivers];
        }
        
        BLOCK_SAFE_CALLS_In_Main_Queue(completeBlock, error);
    }];
}

@end
